﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 税控接口
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            try
            {
                var myProcesses = System.Diagnostics.Process.GetProcessesByName("税控接口");//获取指定的进程名   
                if (myProcesses.Length > 1) //如果可以获取到知道的进程名则说明已经启动
                {
                    MessageBox.Show("程序已启动！");
                    Application.Exit();              //关闭系统
                }
                this.Icon = 税控接口.Properties.Resources.Fly;
                Control.CheckForIllegalCrossThreadCalls = false;
                InitializeComponent();
                RegistryLoad();
                this.btLogin.Click += Login;
                this.btCancel.Click += Cancel;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);
            }
        }


        #region 从注册表读入网址，用户名，密码等信息
        /// <summary>
        /// 初始化时从注册表读入网址，用户名，密码等信息
        /// </summary>
        private void RegistryLoad()
        {
            ServerConfig.ServerAddress = Common.handleRegistry.readKey("tbaddress", "login");
            this.tbAddress.Text = ServerConfig.ServerAddress;
            int icheck = Common.handleRegistry.readKey("cbsave", "login").ObjToInt();
            if (icheck == 1)
            {
                ServerConfig.UserName = Common.handleRegistry.readKey("tbUserNameCustomer", "login");
                this.tbUserName.Text = ServerConfig.UserName;
                ServerConfig.PassWord = Common.handleRegistry.readKey("tbPasswordCustomer", "login");
                this.tbPassword.Text = ServerConfig.PassWord;
                this.cbSave.Checked = true;
            }
            else
            { this.cbSave.Checked = false; }
        }
        private void RegistrySave()
        {

            ServerConfig.ServerAddress = this.tbAddress.Text.Trim();
            ServerConfig.UserName = this.tbUserName.Text.Trim();
            ServerConfig.PassWord = this.tbPassword.Text.Trim();

            Common.handleRegistry.saveKey("tbaddress", ServerConfig.ServerAddress, "login");
            int icheck = 0;
            if (this.cbSave.Checked) { icheck = 1; }
            Common.handleRegistry.saveKey("cbsave", icheck.ToString(), "login");
            if (icheck == 1)
            {
                Common.handleRegistry.saveKey("tbUserNameCustomer", ServerConfig.UserName, "login");
                Common.handleRegistry.saveKey("tbPasswordCustomer", ServerConfig.PassWord, "login");
            }
        }
        #endregion


        private void Login(object sender, EventArgs e)
        {
            try
            {
                RegistrySave();
                ServerConfig.Login();
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("出现异常" + ex.Message + ex.StackTrace);
            }
        }
        private void Cancel(object sender, EventArgs e)
        { this.Close(); }
    }
}
